package ctr_layer;

import java.util.Date;
import java.util.List;

import model_layer.Product;
import model_layer.ProductPart;
import model_layer.Restock;
import model_layer.Supplier;
import db_layer.ProductDB;
import db_layer.RestockDB;
import db_layer.SupplierDB;

public class RestockCtr
{
	private Restock restock;
	private ProductPart current_product;
	private ProductDB prod_db;
	private SupplierDB sup_db;
	private RestockDB res_db;
	
	public RestockCtr()
	{
		restock = null;
		prod_db = new ProductDB();
		sup_db = new SupplierDB();
		res_db = new RestockDB();
	}
	
	public Restock find_restock(int id)
	{
		return res_db.find_restock("id", id, true);
	}
	
	public List<Restock> find_all_restocks()
	{
		return res_db.find_all_restocks(false);
	}
	
	public boolean delete_restock(int id)
	{
		return res_db.delete_restock(id);
	}
	
	public Restock make_new_restock()
	{
		if(restock == null)
		{
			restock = new Restock();
			return restock;
		}
		System.err.println("Previous restock was neither saved nor discarded, this shouldn't happen");
		return null;
	}
	
	public Restock set_supplier_with_id(String sup_id)
	{
		if (restock.getSupplier() == null || !restock.getSupplier().getId().equals(sup_id))
		{
			Supplier sup = sup_db.find_supplier("id", Integer.parseInt(sup_id));
			if(sup != null && sup.getDeleted())
			{
				sup = null;
			}
			restock.setSupplier(sup);
		}
		return restock;
	}
	
	public Restock set_supplier_with_name(String sup_name)
	{	
		if (restock.getSupplier() == null || !restock.getSupplier().getName().equals(sup_name))
		{
			Supplier sup;
			if (sup_name == null || sup_name.equals(""))
			{
				sup = null;
			}
			else
			{
				sup = sup_db.find_supplier("name", sup_name);
			}
			if(sup != null && sup.getDeleted())
			{
				sup = null;
			}
			restock.setSupplier(sup);
		}
		return restock;
	}	
	
	public ProductPart set_product_by_id(String id)
	{
		if(current_product == null)
		{
			current_product = new ProductPart();
		}
		if(restock.get_product_part_by_id(id) != null)
		{
			return null;
		}
		if(!(current_product.getProduct() != null && current_product.getProduct().getId().equals(id)))
		{
			Product prod = prod_db.find_product("id", Integer.parseInt(id));
			if(prod != null && !prod.isDeleted())
			{
				current_product.setProduct(prod);
				current_product.calculate_price();
				return current_product;
			}
			return null;
		}
		return current_product;
	}
	
	public ProductPart set_product_by_name(String name)
	{
		if(current_product == null)
		{
			current_product = new ProductPart();
		}
		if(restock.get_product_part_by_name(name) != null)
		{
			return null;
		}
		else if(name != null && name.length() > 0)
		{
			Product prod = prod_db.find_product("name", name);
			if(prod != null && !prod.isDeleted())
			{
				current_product.setProduct(prod);
				current_product.calculate_price();
				return current_product;
			}
		}
		return null;
	}
	
	public ProductPart set_amount(int amount)
	{
		if(current_product == null)
		{
			current_product = new ProductPart();
		}
		current_product.setAmount(amount);
		current_product.calculate_price();
		return current_product;
	}
	
	public ProductPart set_retail_price(float retail_price)
	{
		if(current_product == null)
		{
			current_product = new ProductPart();
		}
		current_product.setRetail_price(retail_price);
		current_product.calculate_price();
		return current_product;
	}
	
	public ProductPart set_profit(float profit)
	{
		if(current_product == null)
		{
			current_product = new ProductPart();
		}
		if(current_product.getProduct() != null)
		{
			float retail_price = current_product.getRetail_price();
			float sale_price = current_product.getProduct().getPrice();
			if(retail_price != 0 && sale_price != 0)
			{
				current_product.getProduct().setPrice(profit * retail_price / 100 + retail_price);
				return current_product;
			}
		}
		return null;
	}
	
	public ProductPart set_sale_price(float sale_price)
	{
		if(current_product == null)
		{
			current_product = new ProductPart();
		}
		if(current_product.getProduct() != null)
		{
			current_product.getProduct().setPrice(sale_price);
			return current_product;
		}
		return null;
	}
	
	public ProductPart add_current_product_part()
	{
		current_product.calculate_price();
		restock.add_product_part(current_product);
		ProductPart temp = current_product;
		current_product = null;
		return temp;
	}
	
	public ProductPart update_amount(String id, int amount)
	{
		return restock.update_amount(id, amount);
	}
	
	public ProductPart update_product_by_id(String old_id, String id)
	{
		Product prod;
		if(old_id.equals(id))
		{
			return restock.get_product_part_by_id(id);
		}
		else
		{
			prod = prod_db.find_product("id", Integer.parseInt(id));
			if(prod != null && !prod.isDeleted())
			{
				return restock.update_product(old_id, prod);
			}
			return null;
		}
	}

	public ProductPart update_product_by_name(String old_id, String name)
	{
		if(name != null && name.length() > 0)
		{
			ProductPart part = restock.check_for_repeated_insert(old_id, name);
			if(part == null)
			{
				Product prod = prod_db.find_product("name", name);
				if(prod!= null && !prod.isDeleted())
				{
					return restock.update_product(old_id, prod);
				}
			}
			return part;
		}
		return null;
	}
	
	public ProductPart update_retail_price(String id, float retail_price)
	{
		return restock.update_retail_price(id, retail_price);
	}
	
	public ProductPart update_profit(String id, float profit)
	{
		return restock.update_profit(id, profit);
	}
	
	public ProductPart update_sale_price(String id, float sale_price)
	{
		return restock.update_sale_price(id, sale_price);
	}
	
	public Restock get_current_restock()
	{
		restock.calculate_price();
		return restock;
	}
	
	public void remove_product(String id)
	{
		restock.remove_product_part(id);
	}
	
	public boolean save_restock()
	{
		Date receivement_date = new Date();
		restock.setReceivement_date(receivement_date);
		boolean success = res_db.insert_restock(restock);
		if(success)
		{
			restock = null;
		}
		return success;
	}

}
